package com.init.engineering.service.impl;

import com.init.engineering.dao.DeveloperDao;
import com.init.engineering.pages.PageResult;
import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pojo.Developer;
import com.init.engineering.service.DeveloperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class DeveloperServiceImpl implements DeveloperService {

    @Autowired(required = false)
    private DeveloperDao developerDao;

    /**
     * 找到所有承包商信息
     * @return
     */
    @Override
    public List<Developer> findDevelopers() {
        List<Developer> developers = developerDao.findDevelopers();
        return developers;
    }

    @Override
    public Developer findDeveloperByDid(String did) {
        Developer developer = developerDao.findDevByDid(did);
        return developer;
    }

    /**
     * 分页查询所有工程
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findDevelopersByPage(QueryPageBean queryPageBean) {
        /**
         * 获取当前页的第一条数据在数据库内的起始位置
         * 公式:
         *   当前页起始位置 = (当前页 - 起始页(1)) * 分页显示最大条目数
         */
        Integer begin = (queryPageBean.getCurrentPage() - 1) * queryPageBean.getPageSize();
        queryPageBean.setCurrentPage(begin);
        //查询当前页的数据集合
        List<Developer> developers = developerDao.findDevelopersByPage(queryPageBean);
        //获取分页查询的条件
        String queryString = queryPageBean.getQueryString();
        //获取分页查询按条件查询的总条数
        long totalCount = developerDao.selectTotalCount(queryString);

        //将数据封装到PageResult内
        PageResult pageResult = new PageResult();
        pageResult.setTotal(totalCount);
        pageResult.setRows(developers);
        return pageResult;
    }

    /**
     * 删除一个开发商
     * @param did
     * @return
     */
    @Override
    public int deleteDeveloper(String did) {
        int row = developerDao.updateStatusDeveloper(did);
        return row;
    }

    @Override
    public int saveDeveloper(Developer developer) {
        int rows = developerDao.saveDeveloper(developer);
        return rows;
    }

    @Override
    public int UpdateDeveloper(Developer developer) {
        int rows = developerDao.updateDeveloper(developer);
        return rows;
    }
}
